﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MIMDesignShaft
{
    /// <summary>
    /// 调用接口帮助类
    /// </summary>
    class HttpRequest
    {
        /// <summary>
        /// 使用GET方式调用接口
        /// </summary>
        /// <param name="url">接口路径</param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
            request.Accept = "text/html";
            request.ContentType = "application/json";
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
        /// <summary>
        /// 使用POST方式调用接口
        /// </summary>
        /// <param name="url">接口路径</param>
        /// <param name="jsonstr">传入接口的参数</param>
        /// <returns></returns>
        public static string HttpPost(string url, string jsonstr)
        { 
            //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "text/html";
            request.ContentType = "application/json";
            request.Method = "POST";

            byte[] buffer = encoding.GetBytes(jsonstr);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
            {
                return reader.ReadToEnd();
            }
        }
        /// <summary>
        /// 获取结果数据
        /// </summary>
        /// <typeparam name="T">传入参数实体类</typeparam>
        /// <param name="t">传入参数实体对象</param>
        /// <param name="webapiUrl">请求路径</param>
        /// <returns></returns>
        public static string ResultData<T>(T t,string webapiUrl,out string msg)
        {
            //实体类转换为json
            string inStr = JsonConvert.SerializeObject(t);
            //通过http请求获取接口中传出参数
            string resultStr = HttpPost(webapiUrl, inStr);

            JObject jObject = JObject.Parse(resultStr);
            //获取状态码
            string code = jObject["Code"].ToString();
            //获取提示信息
            msg = jObject["Msg"].ToString();
            if (code == "200")
            {
                msg = null;
            }
            //获取传出数据
            string outStr = jObject["Data"].ToString();
            return outStr;
        }
    }
}
